Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data.
!pip install yfinance
#!pip install pandas
#!pip install requests
!pip install bs4
!pip install plotly
Collecting yfinance Using cached yfinance-0.1.59.tar.gz (25 kB) Requirement already satisfied: pandas>=0.24 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from yfinance) (1.2.4) Requirement already satisfied: numpy>=1.15 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from yfinance) (1.20.3) Requirement already satisfied: requests>=2.20 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from yfinance) (2.25.1) Collecting multitasking>=0.0.7 Using cached multitasking-0.0.9.tar.gz (8.1 kB) Requirement already satisfied: lxml>=4.5.1 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from yfinance) (4.6.3) Requirement already satisfied: pytz>=2017.3 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from pandas>=0.24->yfinance) (2021.1) Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from pandas>=0.24->yfinance) (2.8.1) Requirement already satisfied: six>=1.5 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance) (1.15.0) Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from requests>=2.20->yfinance) (1.26.4) Requirement already satisfied: certifi>=2017.4.17 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from requests>=2.20->yfinance) (2020.12.5) Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from requests>=2.20->yfinance) (4.0.0) Requirement already satisfied: idna<3,>=2.5 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from requests>=2.20->yfinance) (2.10) Building wheels for collected packages: yfinance, multitasking Building wheel for yfinance (setup.py): started Building wheel for yfinance (setup.py): finished with status 'done' Created wheel for yfinance: filename=yfinance-0.1.59-py2.py3-none-any.whl size=23442 sha256=3f57312e7fc67208b3ecef4b204879540b9717fac32a8ab15d460bcb9ef095b7 Stored in directory: c:\users\shraavya\appdata\local\pip\cache\wheels\18\0f\5b\f72f0079fdb45935afdd6f56085ac84b6b823a71b3996ba76c Building wheel for multitasking (setup.py): started Building wheel for multitasking (setup.py): finished with status 'done' Created wheel for multitasking: filename=multitasking-0.0.9-py3-none-any.whl size=8368 sha256=ec560ea30092f0c95ea91f0a8d61b5bbee871117ee58f87b823adf33d0612611 Stored in directory: c:\users\shraavya\appdata\local\pip\cache\wheels\1d\13\0b\0c32509050dcd9264e9a90b1d9d2dc9c6db9538db151ea7d26 Successfully built yfinance multitasking Installing collected packages: multitasking, yfinance Successfully installed multitasking-0.0.9 yfinance-0.1.59 Requirement already satisfied: bs4 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (0.0.1) Requirement already satisfied: beautifulsoup4 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from bs4) (4.9.3) Requirement already satisfied: soupsieve>1.2 in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from beautifulsoup4->bs4) (2.2.1) Collecting plotly Using cached plotly-4.14.3-py2.py3-none-any.whl (13.2 MB) Collecting retrying>=1.3.3 Using cached retrying-1.3.3.tar.gz (10 kB) Requirement already satisfied: six in c:\users\shraavya\anaconda3\envs\notebook\lib\site-packages (from plotly) (1.15.0) Building wheels for collected packages: retrying Building wheel for retrying (setup.py): started Building wheel for retrying (setup.py): finished with status 'done' Created wheel for retrying: filename=retrying-1.3.3-py3-none-any.whl size=11429 sha256=338670bb8017589ba4342320dce894831db87959502d1c7b2013217303bf105c Stored in directory: c:\users\shraavya\appdata\local\pip\cache\wheels\ce\18\7f\e9527e3e66db1456194ac7f61eb3211068c409edceecff2d31 Successfully built retrying Installing collected packages: retrying, plotly Successfully installed plotly-4.14.3 retrying-1.3.3
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data.Date, infer_datetime_format=True), y=stock_data.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data.Date, infer_datetime_format=True), y=revenue_data.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.
tesla = yf.Ticker("TSLA")
Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.
tesla_data = tesla.history(period="max")
Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.
tesla_data.reset_index(inplace=True)
tesla_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 | 3.800 | 5.000 | 3.508 | 4.778 | 93831500 | 0 | 0.0 |
| 1 | 2010-06-30 | 5.158 | 6.084 | 4.660 | 4.766 | 85935500 | 0 | 0.0 |
| 2 | 2010-07-01 | 5.000 | 5.184 | 4.054 | 4.392 | 41094000 | 0 | 0.0 |
| 3 | 2010-07-02 | 4.600 | 4.620 | 3.742 | 3.840 | 25699000 | 0 | 0.0 |
| 4 | 2010-07-06 | 4.000 | 4.000 | 3.166 | 3.222 | 34334500 | 0 | 0.0 |
Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue. Save the text of the response as a variable named html_data.
url="https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
html_data= requests.get(url).text
Parse the html data using beautiful_soup.
soup = BeautifulSoup(html_data, "html.parser")
soup.find_all('title')
[<title>Tesla Revenue 2009-2021 | TSLA | MacroTrends</title>]
Using beautiful soup extract the table with Tesla Quarterly Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column.
tesla_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])
for row in soup.find_all("tbody")[1].find_all("tr"):
col = row.find_all("td")
date = col[0].text
revenue = col[1].text.replace("$", "").replace(",", "")
tesla_revenue = tesla_revenue.append({"Date": date, "Revenue": revenue}, ignore_index = True)
Remove the rows in the dataframe that are empty strings or are NaN in the Revenue column. Print the entire tesla_revenue DataFrame to see if you have any.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.
tesla_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 42 | 2010-09-30 | 31 |
| 43 | 2010-06-30 | 28 |
| 44 | 2010-03-31 | 21 |
| 46 | 2009-09-30 | 46 |
| 47 | 2009-06-30 | 27 |
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.
GameStop = yf.Ticker("GME")
Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.
gme_data = GameStop.history(period = 'max')
Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.
gme_data.reset_index(inplace = True)
gme_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 | 6.480513 | 6.773399 | 6.413183 | 6.766666 | 19054000 | 0.0 | 0.0 |
| 1 | 2002-02-14 | 6.850831 | 6.864296 | 6.682506 | 6.733003 | 2755400 | 0.0 | 0.0 |
| 2 | 2002-02-15 | 6.733001 | 6.749833 | 6.632006 | 6.699336 | 2097400 | 0.0 | 0.0 |
| 3 | 2002-02-19 | 6.665671 | 6.665671 | 6.312189 | 6.430017 | 1852600 | 0.0 | 0.0 |
| 4 | 2002-02-20 | 6.463681 | 6.648838 | 6.413183 | 6.648838 | 1723200 | 0.0 | 0.0 |
Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue. Save the text of the response as a variable named html_data.
url = "https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue"
html_data = requests.get(url).text
Parse the html data using beautiful_soup.
soup = BeautifulSoup(html_data, "html.parser")
soup.find_all('title')
[<title>GameStop Revenue 2006-2021 | GME | MacroTrends</title>]
Using beautiful soup extract the table with GameStop Quarterly Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.
gme_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])
for row in soup.find_all("tbody")[1].find_all("tr"):
col = row.find_all("td")
date = col[0].text
revenue = col[1].text.replace("$", "").replace(",", "")
gme_revenue = gme_revenue.append({"Date": date, "Revenue": revenue}, ignore_index = True)
Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
gme_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 60 | 2006-01-31 | 1667 |
| 61 | 2005-10-31 | 534 |
| 62 | 2005-07-31 | 416 |
| 63 | 2005-04-30 | 475 |
| 64 | 2005-01-31 | 709 |
Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla')
make_graph(tesla_data, tesla_revenue, 'Tesla')
Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop').
make_graph(gme_data, gme_revenue, 'GameStop')
| Date (YYYY-MM-DD) | Version | Changed By | Change Description |
|---|---|---|---|
| 2020-11-10 | 1.1 | Malika Singla | Deleted the Optional part |
| 2020-08-27 | 1.0 | Malika Singla | Added lab to GitLab |